package com.yusheng.tr.multi.controller;

import com.yusheng.domain.R;
import com.yusheng.tr.common.core.model.dto.multi.StatementDto;
import com.yusheng.tr.common.core.model.dto.multi.StmtMngDto;
import com.yusheng.tr.common.core.page.PageParam;
import com.yusheng.tr.common.core.utils.minio.MinioUtils;
import com.yusheng.tr.common.datascope.annotation.DataScope;
import com.yusheng.tr.multi.annotation.ReportLog;
import com.yusheng.tr.common.core.entity.multi.StmtMngEntity;
import com.yusheng.tr.multi.enums.LogEnum;
import com.yusheng.tr.multi.service.IStmtMngService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

/**
 * <p>
 * 结算单管理表 前端控制器
 * </p>
 *
 * @author author
 * @since 2023-10-07
 */
@RestController
@RequestMapping("/stmt-mng")
@Tag(name = "结算单管理",description = "结算单管理")
public class StmtMngController {

    @Autowired
    private IStmtMngService stmtMngService;

    @Resource
    private MinioUtils minioUtilS;

    @PostMapping("/add/or/update")
    @ReportLog(title = "#mng.id", logType = LogEnum.ADD, content = "#mng.stmtContent")
    @Operation(summary = "新增结算单管理",description = "新增结算单管理")
    @Parameter(name = "mng",description = "结算单实体参数")
    public R<?> addOrUpdate(@Valid @RequestBody StmtMngEntity mng) {
        stmtMngService.save(mng);
        return R.ok(mng.getId());
    }

    @ReportLog(title = "#stmtMngEntity.id", logType = LogEnum.UPDATE, content = "#stmtMngEntity.stmtContent")
    @PutMapping("/update/stmt")
    @Operation(summary = "更新修改结算单",description = "更新修改结算单")
    @Parameter(name = "stmtMngEntity",description = "结算单参数")
    public R update(@Valid @RequestBody StmtMngEntity stmtMngEntity) {
        stmtMngService.updateById(stmtMngEntity);
        return R.ok();
    }

    @ReportLog(title = "附件上传", logType = LogEnum.ANNEX)
    @PutMapping("/update/stmt/upload")
    @Operation(summary = "附件上传",description = "附件上传")
    @Parameter(name = "stmtMngEntity",description = "结算单参数")
    public R updateAnnex(@Valid @RequestBody StmtMngEntity stmtMngEntity) {
        stmtMngService.updateById(stmtMngEntity);
        return R.ok();
    }

    @GetMapping("/delete/by/{id}")
    @ReportLog(title = "删除结算单", logType = LogEnum.OTHER)
    @Operation(summary = "删除结算单",description = "删除结算单")
    @Parameter(name = "id",description = "要删除的结算单id")
    public R<?> deleteById(@PathVariable String id) {
        stmtMngService.removeById(id);
        return R.ok();
    }

    @PostMapping("/page/list")
    @DataScope(deptAlias = "d", userAlias = "u")
    @Operation(summary = "分页条件查询",description = "分页条件查询")
    @Parameter(name = "dto",description = "分页条件")
    public R<?> pageList(@RequestBody PageParam<StmtMngDto> dto) {
        return R.ok(stmtMngService.pageList(dto));
    }


    @PostMapping("/upload")
    @Operation(summary = "上传文件到minio",description = "上传文件到minio")
    @Parameters({@Parameter (name = "file",description = "要上传的文件"),
                 @Parameter(name = "bucketName",description = "所要上传的桶名")
    })
    public R<?> upload(@RequestPart("file") MultipartFile file, @RequestParam("bucketName") String bucketName) throws Exception {
        return R.ok(minioUtilS.uploadFile(file, bucketName));
    }

    @GetMapping("/export")
    @Operation(summary = "导出结算单pdf",description = "导出结算单pdf")
    @Parameter(name = "id",description = "要导出结算单pdf的id")
    public R<?> test(String id) throws Exception {
        return R.ok(stmtMngService.testExcel(id));
    }

    @GetMapping("/sui/{id}")
    @Operation(summary = "导出随钻的pdf",description = "导出随钻的pdf")
    @Parameter(name = "id",description = "要导出随钻pdf的id")
    public R export(@PathVariable String id) throws Exception {
        return R.ok(stmtMngService.export(id));
    }

    @PostMapping("/test/statement")
    @Operation(summary = "导出测井结算单统计excel明细表",description = "导出测井结算单统计excel明细表")
    @Parameter(name = "dto",description = "批量导出的参数")
    public R<?>  testExport(@RequestBody StatementDto dto) throws Exception {
        return R.ok(stmtMngService.statementExport(dto.getIds(),dto.getStartTime(),dto.getEndTime()));
    }
    @GetMapping("/qiao/{id}")
    @Operation(summary = "导出桥射联作施工单pdf",description = "导出桥射联作施工单pdf")
    @Parameter(name = "id",description = "要导出桥射联作施工单pdf的id")
    public R exportQiao(@PathVariable String id) throws Exception {
        return R.ok(stmtMngService.exportQiao(id));
    }

    @PostMapping("/test/sui")
    @Operation(summary = "导出随钻excel明细表",description = "导出随钻excel明细表")
    @Parameter(name = "dto",description = "批量导出的参数")
    public R testSuiExcel(@RequestBody StatementDto dto) throws Exception {
        return R.ok(stmtMngService.testSuiExcel(dto));
    }


    @PostMapping("/test/bridgeShootSheet")
    @Operation(summary = "导出桥射联作施工单excel明细表",description = "导出桥射联作施工单excel明细表")
    @Parameter(name = "dto",description = "批量导出的参数")
    public R  exportBridgeShootSheet(@RequestBody StatementDto dto) throws Exception {
        return R.ok(stmtMngService.exportBridgeShootSheet(dto.getIds(),dto.getStartTime(),dto.getEndTime()));
    }
    @GetMapping("/tubing/{id}")
    @Operation(summary = "导出连续油管pdf",description = "导出连续油管pdf")
    @Parameter(name = "id",description = "要导出pdf的id")
    public R exportTubing(@PathVariable String id) throws Exception {
        return R.ok(stmtMngService.exportTubing(id));
    }

    @PostMapping("/testWellSheet/{id}")
    @Operation(summary = "导出生产测井施工结算单pdf",description = "导出生产测井施工结算单pdf")
    @Parameter(name = "id",description = "要导出pdf的id")
    public R exportTestWellSheet(@PathVariable  String id) throws Exception{
        return R.ok(stmtMngService.exportTestWellSheet(id));
    }

    @GetMapping("/beam/{id}")
    @Operation(summary = "导出射孔施工单pdf",description = "导出射孔施工单pdf")
    @Parameter(name = "id",description = "要导出pdf的id")
    public R exportBeam(@PathVariable String id) throws Exception {
        return R.ok(stmtMngService.exportBeam(id));
    }

    @PostMapping("/export/testWellExcel")
    @Operation(summary = "导出生产测井施工单excel统计表",description = "导出生产测井施工单excel统计表")
    @Parameter(name = "dto",description = "批量导出的参数")
    public R exportBeam(@RequestBody StatementDto dto) throws Exception{
        return R.ok(stmtMngService.exportTestWellExcel(dto.getIds(),dto.getStartTime(),dto.getEndTime()));
    }
    @PostMapping("/she")
    @Operation(summary = "导出射孔单井施工单统计excel明细表",description = "导出射孔单井施工单统计excel明细表")
    @Parameter(name = "dto",description = "批量导出的参数")
    public R exportShe(@RequestBody StatementDto dto) throws Exception{
        return R.ok(stmtMngService.exportShe(dto.getIds(),dto.getStartTime(),dto.getEndTime()));
    }

    @PostMapping("/lian")
    @Tag(name="导出连续油管单井施工单统计表excel")
    @Parameter(name = " StatementDto dto",description = "三个参数：ids , startTime ,endTime")
    public R exportCoiledTube(@RequestBody StatementDto dto) throws Exception {
        return R.ok(stmtMngService.exportCoiledTubeExcel(dto.getIds(), dto.getStartTime(), dto.getEndTime()));
    }
}
